
//
//  Extension+UIImage.swift
//  WJWeiBo
//
//  Created by wangjun on 16/7/28.
//  Copyright © 2016年 wangjun. All rights reserved.
//

import UIKit

extension UIImage {
    
    static func getScreenShot()->UIImage {
       
        //得到windows
        let windows = UIApplication.sharedApplication().keyWindow!
        //1.开启图片上下文
        UIGraphicsBeginImageContext(windows.bounds.size)
        //2.将图片渲染到上下文中
        windows.drawViewHierarchyInRect(windows.bounds, afterScreenUpdates: false)
        //3.从上下文中获取image
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //4.关闭
        UIGraphicsEndImageContext()
        return image
    
    }
    
    //压缩图片
    func getImageScale(width: CGFloat) ->UIImage {
        
        if self.size.width < width {
            
            return self
        }
        
        let height = width * self.size.height / self.size.width
        
        let frame = CGRect(x: 0, y: 0, width: width, height: height)
        //开启图片上下文
        UIGraphicsBeginImageContext(frame.size)
        //将图片渲染到上下文
        self.drawInRect(frame)
        //获取图片
       let result =  UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        
        return result
      
    }
    
}